package com.cars.satoken.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 封禁控制器
 *
 * @author: caoqianfan
 * @date: 2023/11/21 9:12
 */
@RestController
@RequestMapping("/disable")
public class DisableController {
    /**
     * 封禁账号
     *
     * @param loginId 账号id
     * @param service 指定服务
     * @param level   封禁级别
     * @param time    封禁时长
     * @return {@code SaResult}
     */
    @PostMapping("/account")
    public SaResult account(Integer loginId, @RequestParam(required = false) String service, @RequestParam(required = false) Integer level, long time) {
        // 先踢下线
        if (StpUtil.isLogin(loginId)) {
            StpUtil.kickout(loginId);
        }
        // 再封禁账号
        if (service.isBlank()) {
            // 登录封禁
            if (level == null) {
                if (!StpUtil.isDisable(loginId)) {
                    StpUtil.disable(loginId, time);
                }
            } else {
                if (!StpUtil.isDisableLevel(loginId, level)) {
                    StpUtil.disableLevel(loginId, level, time);
                }
            }
        } else {
            // 服务封禁
            if (level == null) {
                if (!StpUtil.isDisable(loginId, service)) {
                    StpUtil.disable(loginId, service, time);
                }
            } else {
                if (!StpUtil.isDisableLevel(loginId, service, level)) {
                    StpUtil.disableLevel(loginId, service, level, time);
                }
            }
        }
        return SaResult.ok(service.isBlank() ? "" : "【" + service + "】" + "已封禁id为" + loginId + "的账号，时长为" + time + "秒");
    }

    /**
     * 获取：指定账号的指定服务 剩余封禁时间，单位：秒（-1=永久封禁，-2=未被封禁）
     *
     * @param loginId 账号id
     * @param service 指定服务
     * @return {@code SaResult}
     */
    @GetMapping("/getDisableTime")
    public SaResult getDisableTime(Integer loginId, @RequestParam(required = false) String service) {
        long time;
        if (service.isBlank()) {
            time = StpUtil.getDisableTime(loginId);
        } else {
            time = StpUtil.getDisableTime(loginId, service);
        }
        if (time == -1) {
            return SaResult.ok("id为" + loginId + "的账号已被永久封禁");
        }
        if (time == -2) {
            return SaResult.ok("id为" + loginId + "的账号没有被封禁");
        }
        return SaResult.ok(service.isBlank() ? "" : "【" + service + "】" + "id为" + loginId + "的账号，剩余封禁时长为" + time + "秒");
    }

    /**
     * 获取：指定账号的指定服务 封禁的级别 （如果此账号未被封禁则返回 -2）
     *
     * @param loginId 账号id
     * @param service 指定服务
     * @return {@code SaResult}
     */
    @GetMapping("/getDisableLevel")
    public SaResult getDisableLevel(Integer loginId, @RequestParam(required = false) String service) {
        int level;
        if (service.isBlank()) {
            level = StpUtil.getDisableLevel(loginId);
        } else {
            level = StpUtil.getDisableLevel(loginId, service);
        }
        if (level == -2) {
            return SaResult.ok("id为" + loginId + "的账号没有被封禁");
        }
        return SaResult.ok(service.isBlank() ? "" : "【" + service + "】" + "id为" + loginId + "的账号，当前封禁级别为" + level + "级");
    }

    /**
     * 解封服务
     *
     * @param loginId  账号id
     * @param services 指定服务，可以指定多个
     * @return {@code SaResult}
     */
    @PostMapping("/untieDisable")
    public SaResult untieDisable(Integer loginId, @RequestParam(required = false) String[] services) {
        StpUtil.untieDisable(loginId, services);
        return SaResult.ok(services == null ? "" : "【" + Arrays.toString(services) + "】" + "已解封id为" + loginId + "的账号");
    }

}